---
title: "BRI maps"
author: "Christoph Valentin Steinert"
date: "3/21/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = FALSE)
```

Packages:

```{r}
library(readxl)
library(tidyverse)
library(giscoR)
library(lubridate)
library(zoo)
library(sf)
```

Set up the data

```{r}
rm(list = ls())
bri_data <- read_xlsx("BRI_Dates_byCountry.xlsx", 2)
```

Select relevant variables and prepare date-variable

```{r}
bri_data_sub <- select(bri_data, Country, Iso, Date_MoU_Signed)
bri_data_sub$Date_MoU_Signed <- as.Date(bri_data_sub$Date_MoU_Signed, format = c("%d.%m.%Y"))
bri_data_sub$Signed_Month <- as.yearmon(bri_data_sub$Date_MoU_Signed)
bri_data_sub$Signed_Year <- format(bri_data_sub$Date_MoU_Signed, format="%Y")

```

Prepare world map data

```{r}
countries <- gisco_get_countries(
  year = "2020",
  epsg = "4326",
  resolution = "10"
)

continents <- gisco_countrycode

countries <- countries %>% left_join(continents, by = "ISO3_CODE")

countries <- subset(countries, CNTR_NAME != "Antarctica")
countries_small <- select(countries, CNTR_ID, continent, un.regionsub.name, un.regionintermediate.name, geometry) %>% 
  rename(Iso = CNTR_ID)

unique(countries$un.regionintermediate.name)
```
Merge datasets

```{r}
# Check failures
merge.fail1 <- anti_join(countries_small, bri_data_sub, by="Iso") # in map-data but not in bri-data 
merge.fail2 <- anti_join(bri_data_sub, countries_small, by="Iso") # in bri-data but not in map-data 

# Recode failed merges
bri_data_sub$Iso[bri_data_sub$Country == "Greece"] <- "EL"
bri_data_sub$Iso[bri_data_sub$Country == "United Kingdom"] <- "UK"

# Check failures
merge.fail1 <- anti_join(countries_small, bri_data_sub, by="Iso") # in map-data but not in bri-data: only disputed territories (irrelevant here because they cannot sign BRI agreements) 

# Merge
mapdata <- merge(countries_small, bri_data_sub, by = "Iso", all.x = T, all.y = F)

# Code as factor
mapdata$Signed_Year <- as.factor(mapdata$Signed_Year)

```

Prepare maps

```{r}
#### Reproduces Figure 1 ####
mapdata <- mapdata %>% mutate(china = ifelse(Country == "China", "Initiated", "Not Signed"))

ggplot(data = mapdata$geometry) + 
  geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = mapdata$Signed_Year), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  labs(fill = "Year of Signature") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))

#ggsave("./figures/worldmap.png")

ggplot(data = mapdata$geometry) + 
  geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = mapdata$Signed_Year), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  labs(fill = "Year of Signature") +
  theme_void() + 
  theme(legend.position = "none"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))


#ggsave("./figures/worldmap_no_legend.png")

```

# Europe

```{r}
#### Reproduces Figure A.4 ####

# Specify the bottom left and top right corner 
# longitude and latitude in degrees
# based on the WGS84 standard 
# (using the EPSG code 4326)
limits <- st_sfc(st_point(c(-18, 32))
                 , st_point(c(60, 62))
                 , crs = 4326)

# Transform the spatial data 
# to EPSG 3035 coordinates
limits <- st_transform(limits, crs = 3035)

# Get coordinates
limits <- st_coordinates(limits)
limits

europe <- mapdata %>% filter(continent == "Europe" | continent == "Africa" | un.regionsub.name == "Western Asia")


ggplot(europe) +
  geom_sf(aes(fill = europe$china), size = 0.02) +

  geom_sf(aes(fill = europe$Signed_Year), size = 0.02) +
  geom_sf(data = mapdata, fill = "transparent") + 
  coord_sf(xlim = limits[,'X'], ylim = limits[,'Y'],  crs = st_crs(3035)) +
  scale_fill_viridis_d() +
  labs(fill = "Year of Signature",
       title = "Europe") +
  theme(legend.position = "bottom") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))


#ggsave("./figures/europe.png")



```


# Africa

```{r}
#### Reproduces Figure A.2 ####

africa <- mapdata %>% filter(continent == "Africa")

ggplot(data = africa$geometry) + 
  geom_sf(aes(fill = africa$china), size = 0.02) +

  geom_sf(aes(fill = africa$Signed_Year), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  labs(fill = "Year of Signature",
       title = "Africa") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))

#ggsave("./figures/africa.png")
```

## South-East Asia
```{r}
#### Reproduces Figure A.3 ####
data <- mapdata  %>% filter(un.regionsub.name == "South-eastern Asia")


ggplot(data = data$geometry) + 
  geom_sf(aes(fill = data$china), size = 0.02) +

  geom_sf(aes(fill = data$Signed_Year), size = 0.02) +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  labs(fill = "Year of Signature",
       title = data$un.regionsub.name) +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))


#ggsave("./figures/south_east_asia.png")

```


```{r}
#### Reproduces Figure A.1 ####
data <- mapdata  %>% filter(un.regionsub.name == "Latin America and the Caribbean")

ggplot(data = data$geometry) + 
  geom_sf(aes(fill = data$china), size = 0.02) +

  geom_sf(aes(fill = data$Signed_Year), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  scale_color_manual(name = "Year of Signature",
                     values = c("Initiated" = "red", 
                                "Not Signed" = "white")) +
  labs(title = data$un.regionsub.name) +
  labs(fill = "Year of Signature") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))


#ggsave("./figures/latin_america.png")
```


```{r}
data <- mapdata  %>% filter(un.regionintermediate.name == "Caribbean")


ggplot(data = data$geometry) + 
  geom_sf(aes(fill = data$china), size = 0.02) +

  geom_sf(aes(fill = data$Signed_Year), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +
  scale_color_manual(name = "Year of Signature",
                     values = c("Initiated" = "red", 
                                "Not Signed" = "white")) +
  labs(title = data$un.regionintermediate.name) +
  labs(fill = "Year of Signature") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm"))


#ggsave("./figures/caribbean.png")
```

## Year plots


```{r}


data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2014, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2013") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2013.png")


data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2015, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2014") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2014.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2016, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2015") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2015.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2017, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2016") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2016.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2018, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2017") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2017.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2019, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2018") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2018.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2020, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2019") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2019.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2021, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2020") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2020.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2022, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2021") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2021.png")




data <- mapdata %>% 
  mutate(year_num = as.numeric(as.character(Signed_Year)),
         signed = ifelse(year_num < 2023, 1, 0), 
         signed = ifelse(is.na(signed), 0, signed), 
         signed = as.factor(signed))

ggplot(data = data$geometry) + 
  #geom_sf(aes(fill = mapdata$china), size = 0.02) +

  geom_sf(aes(fill = data$signed), size = 0.02) +
  #scale_fill_discrete(name = "Year BRI \n entry") +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  scale_fill_viridis_d() +

  labs(fill = "Signed", 
       title = "2022") +
  theme_void() + 
  theme(legend.position = "bottom"
        , panel.background = element_rect(fill = "aliceblue")
        , plot.title = element_text(face = "bold")
        , legend.key.width = unit(1, "cm")
        , legend.key.size = unit(.5, "cm")
        )


#ggsave("./figures/2022.png")





```

```{r}
# end
```
